#include<vector>
using namespace std;


class Solution {
private:
    const int going = INT32_MAX;

    bool helper(vector<int>& nums, int lastIndex,int curIndex,bool dir) {
        if (nums[curIndex] == going)
            return lastIndex != curIndex;
        if (nums[curIndex] == 0 || ((nums[curIndex] > 0) ^ dir))
            return false;
        lastIndex = curIndex;
        int size = nums.size();
        curIndex = ((curIndex + nums[curIndex]) % size + size) % size;
        nums[lastIndex] = going;
        if (!helper(nums, lastIndex, curIndex, dir)) {
            nums[lastIndex] = 0;
            return false;
        }
        return true;
    }

public:
    bool circularArrayLoop(vector<int>& nums) {
        for (int i = 0; i < nums.size(); ++i) {
            if (helper(nums, INT32_MAX, i, nums[i] > 0))
                return true;
        }
        return false;
    }
};